<?php
/**
 * 绑定会员卡
 */
namespace Weixin\Controller;

use Common\Controller\WeixinbaseController;
use Think\Model;

class BindCardController extends WeixinbaseController
{
    protected $wx_stuCard;  //学习卡
	protected  $articlemo;  //文章
    
    public function _initialize()
    {
    		$this->wx_stuCard = D("Common/StuCard");
//     		$this->articlemo = D("Common/ArticleMo");
    		
       		 parent::_initialize();
    }


    public function index(){
        $open_id = $_SESSION['openID'];
        $this->display();
    }

    /**
     * @info 学习卡绑定操作
     */
    public function bindCard_ajax()
    {
        if (IS_AJAX) {
        	$open_id = $_SESSION['openID'];
            $number = $_POST['number'];
            $password = $_POST['password'];
            
            if(!empty($open_id)){
            	if(!empty($number) && isset($number{6})){
            		if(!empty($password) && isset($password{3})){
		            			$data['open_id'] = $open_id;
		            			$data['username']= $number;
		            			$data['password'] = $password;
		            			$data['create_time'] = time();
		            			//写入数据库
		            			if(false !== $this->wx_stuCard->add($data)){
		            				$this->ajaxReturn([
		            						'status' => 1
		            				]);
		            			}
            		}else{
			            	$this->ajaxReturn([
			            			'status' => 0,
			            			'msg'  => '密码必须为4位'
			            	]);
			            }
            	}else{
	            	$this->ajaxReturn([
	            			'status' => 0,
	            			'msg'  => '卡号必须为7位'
	            	]);
	            }
            }else{
            	$this->ajaxReturn([
            			'status' => 0,
            			'msg'  => '程序出了个小差，请重试'
            	]);
            }
			            	
        }
    }

    /**
     *  @info  学习中心 
     */
    public function stuCenter()
    {
        $model = new Model() ;
    	$banner_data = $model->db(1,"mysql://root:root@127.0.0.1:3306/trnlwx")->query("SELECT * FROM wp_indexbanner");
    	$list_data = $model->db(1,"mysql://root:root@127.0.0.1:3306/trnlwx")->query("SELECT * FROM wp_article");
    	
    	foreach ($banner_data as $k=>$val){
    		$picture = $val['picture'];
    		$banner_data[$k]['picture'] = $this->get_cover_url($picture);
    	}
    	
    	$this->assign('banner_data',$banner_data);
    	$this->assign('list_data',$list_data);
    	 
    	$this -> assign($list_data);
    	$this->display();
    }
    
    /**
     * 文章详情
     */
    public function detail()
    {
    	$id = I('id');
    	$model = new Model() ;
    	$data = $model->db(1,"mysql://root:root@127.0.0.1:3306/trnlwx")->query("SELECT * FROM wp_article where id=".$id);
    	
        $this->assign("data", $data[0]);
        $this->display("BindCard/detail");
    }
    
    
    function get_cover_url($cover_id, $width = '', $height = '') {
    	$info = $this->get_cover ( $cover_id );
    
    	if ($width > 0 && $height > 0) {
    		$thumb = "?imageMogr2/thumbnail/{$width}x{$height}";
    	} elseif ($width > 0) {
    		$thumb = "?imageMogr2/thumbnail/{$width}x";
    	} elseif ($height > 0) {
    		$thumb = "?imageMogr2/thumbnail/x{$height}";
    	}
    	if ($info ['url'])
    		return $info ['url'] . $thumb;
    
    		$url = $info ['path'];
    		if (empty ( $url ))
    			return '';
    			return $url . $thumb;
    }
    
    /**
     * 获取文档封面图片
     *
     * @param int $cover_id
     * @param string $field
     * @return 完整的数据 或者 指定的$field字段值
     * @author huajie <banhuajie@163.com>
     */
    function get_cover($cover_id, $field = null) {
    	if (empty ( $cover_id ))	
    		return false;
    		$key = 'Picture_' . $cover_id;
    		$picture = S ( $key,null );
    
    		if (! $picture) {
//     			$map ['status'] = 1;
//     			$picture = M ( 'Picture' )->where ( $map )->getById ( $cover_id );
    			$model = new Model() ;
    			$picture = $model->db(1,"mysql://root:root@127.0.0.1:3306/trnlwx")->query("SELECT * FROM wp_picture where status=1 AND id=".$cover_id);
    			$picture = $picture[0];
    			$sql = $model ->getLastSql();
    			S ( $key, $picture, 86400 );
    		}
    		if (empty ( $picture ))
    			return '';
    			return empty ( $field ) ? $picture : $picture [$field];
    }
}

